home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / LibraryOfCongress / LibraryOfCongress.app / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  5.9 KB  |  303 lines

  1. /*
  2.  * A potpourri of handy little system utilities.
  3.  *
  4.  * Michael Hawley
  5.  * MIT Media Laboratory
  6.  * 20 Ames St
  7.  * Cambridge, MA 02139
  8.  * mike@media-lab.mit.edu
  9.  * Copyright (c) MIT Media Laboratory 1991
  10.  */
  11. #include "util.h"
  12. #include <sys/stat.h>
  13.  
  14. extern free(), access(), mkdir(), system(), 
  15.        strlen(), strcpy(), strcmp(), strncmp();
  16.  
  17. extern char *index(), *rindex();
  18.  
  19. char *av0, *_arg, *_argp; /* use by 'for_each_argument */
  20.  
  21. void
  22. error(a,b,c,d,e,f) int a,b,c,d,e,f; { /* printf an error msg */
  23.     printf((char *)a,b,c,d,e,f); printf("\n");
  24. }
  25.  
  26. int Verbose=0;
  27.  
  28. void
  29. debug(a,b,c,d) int a,b,c,d; { /* printf an error msg */
  30.     if (Verbose) printf((char *)a,b,c,d), printf("\n");
  31. }
  32.  
  33. int
  34. System(fmt, a,b,c,d,e,f,g)
  35.     char *fmt,*a,*b,*c,*d,*e,*f,*g;
  36. /*
  37.  * "printf" a system call, gripe if it failed.
  38.  */
  39. {
  40.     char t[2048];
  41.     int i=0;
  42.     sprintf(t,fmt,a,b,c,d,e,f,g);
  43.     debug("!%s",t);
  44.     if (i = system(t)) error("%s: failed! %s",av0,t);
  45.     return !i;
  46. }
  47.  
  48. char *
  49. save(s) char *s; { /* save a copy of 's' and return the pointer */
  50.     char *t = (char *)malloc(strlen(s)+1);
  51.     if (t) strcpy(t,s);
  52.     return t;
  53. }
  54.  
  55. int
  56. blank(s) char *s; { /* true if 's' is blank */
  57.     while (*s == ' ' || *s=='\t' || *s == '\n' || *s == '\r' || *s == 27) ++s;
  58.     return (int)!*s;
  59. }
  60.  
  61. void
  62. stripcomment(s) char *s; { /* truncate 's' at a comment character ('#') */
  63.     char *p = index(s,'#');
  64.     if (p && (p==s || p[-1] != '\\')) *p = '\0';
  65. }
  66.  
  67. #ifdef 0
  68. void
  69. stripnl(s) char *s; { /* remove trailing \n and space from 's' */
  70.     char *p = s + strlen(s)-1;
  71.     while (p>s && (*p=='\n' || *p == ' ' || *p=='\r')) *p-- = '\0';
  72. }
  73. #endif
  74.  
  75. char *
  76. skipsp(s) char *s; {
  77.     while (*s==' ' || *s=='\t' || *s == '\n') ++s;
  78.     return s;
  79. }
  80.  
  81. void
  82. squishblank(s) char *s; {
  83.     char *t=s, *start=s;
  84.     while (*t = *s){
  85.         if (t>start && (*t==' ' || *t=='\t') && (t[-1]==' '||t[-1]=='\t'))
  86.             ++s;
  87.         else
  88.             ++t, ++s;
  89.     }
  90. }
  91.  
  92. void
  93. squishwhite(s) char *s; {
  94.     char *t=s, *start=s;
  95.     while (*t = *s){
  96.         if (t>start && (*t==' ' || *t=='\t' || *t=='\n') && 
  97.                    (t[-1]==' '||t[-1]=='\t'||t[-1]=='\n'))
  98.             ++s;
  99.         else
  100.             ++t, ++s;
  101.     }
  102. }
  103.  
  104. char *
  105. prefix(s,t) char *s, *t; { /* true if 't' is a prefix of 's' */
  106.     int sl = strlen(s);
  107.     t = skipsp(t);
  108.     return (*t == *s && strncmp(s,t,sl)==0)? skipsp(t+sl) : (char *)0;
  109. }
  110.  
  111. int
  112. suffix(s,t) char *s, *t; { /* true if 't' is a suffix of 's' */
  113.     s = rindex(s,'.');
  114.     return s? strcmp(s,t)==0 : 0;
  115. }
  116.  
  117. strtolower(s) char *s; {
  118.     while (*s){
  119.         if (isupper(*s)) *s = tolower(*s);
  120.         s++;
  121.     }
  122. }
  123.  
  124. char *
  125. strindex(s,t) char *s, *t; { /* return ptr to first match of 't' in 's' */
  126.     int n = strlen(t);
  127.  
  128.     if (s)
  129.         while (*s)
  130.             if (!strncmp(s, t, n))
  131.                 return s;
  132.             else
  133.                 s++;
  134.     return (char *)0;
  135. }
  136.  
  137. char *
  138. stripnondigit(s) char *s; {
  139.     char *start = s, *p = s;
  140.  
  141.     while (*p){
  142.         if (isdigit(*p)) *s++ = *p;
  143.         p++;
  144.     }
  145.     *s = '\0';
  146.     return start;
  147. }
  148.  
  149. char *
  150. strippunct(s) char *s; {
  151.     char *start = s, *p = s;
  152.  
  153.     while (*p){
  154.         if (ispunct(*p)) ;
  155.         else *s = *p;
  156.         s++, p++;
  157.     }
  158.     return start;
  159. }
  160.  
  161. void
  162. sub(s,a,b) char *s,a,b; { /* in 's', s/a/b/g */
  163.     while (*s){
  164.         if (*s==a) *s=b;
  165.         s++;
  166.     }
  167. }
  168.  
  169. void
  170. substr(s,a,b) char *s, *a, *b; { /* like 'sub', but with strings */
  171.     char q[8192];
  172.     char *p = s;
  173.     int n = strlen(a);
  174.     for (;*p;p++){
  175.         if (*p == *a && strncmp(p,a,n)==0){
  176.             strcpy(q,p+n);
  177.             strcpy(p,b);
  178.             strcpy(p+strlen(b),q);
  179.             p += strlen(b)-1;
  180.         }
  181.     }
  182. }
  183.  
  184. void
  185. stot(s,t,c) char *s, **t, c; { /* split 's' into a table 't' */
  186.     char *p;
  187.     while (p = index(s,c)){
  188.         *p++ = '\0';
  189.         *t++ = s;
  190.         s = p;
  191.     }
  192.     *t++ = s;
  193.     *t = (char *)0;
  194. }
  195.  
  196. #include <sys/stat.h>
  197.  
  198. int fMode(f) char *f; { /* return mode bits */
  199.     struct stat b;
  200.     return stat(f,&b)? 0 : b.st_mode;
  201. }
  202.  
  203. int fSize(f) char *f; { /* return mode bits */
  204.     struct stat b;
  205.     return stat(f,&b)? 0 : b.st_size;
  206. }
  207.  
  208. int fTime(f) char *f; { /* mod time of file 'f' */
  209.     struct stat b;
  210.     return stat(f,&b)? 0 : b.st_mtime;
  211. }
  212.  
  213. int fDirectory(f) char *f; { /* true if file 'f' is a directory */
  214.     struct stat b;
  215.     return stat(f,&b)? 0 : ((b.st_mode & S_IFDIR) == S_IFDIR);
  216. }
  217.  
  218. int fLink(f) char *f; { /* true if file 'f' is a symbolic link */
  219.     struct stat b;
  220.     return lstat(f,&b)? 0 : ((b.st_mode & S_IFLNK) == S_IFLNK);
  221. }
  222.  
  223. int
  224. mkdirs(s,mode)
  225.     char *s;
  226.     int mode;
  227. { /* ensure that all the directories in 's' exist */
  228.     char t[4096], *p = t;
  229.  
  230.     strcpy(t,s);
  231.     if (*p=='/') ++p;
  232.     while (p = index(p,'/')) {
  233.     *p = '\0';
  234.         if (access(t,0)){
  235.             if (mkdir(t,mode))
  236.                 return 0;
  237.         /* fixmod(t); */
  238.     }
  239.         *p++ = '/';
  240.     }
  241.     if (access(t,0) && mkdir(t,mode)) return 0;
  242.     /* fixmod(t); */
  243.     debug("mkdir %s",t);
  244.     return 1;
  245. }
  246.  
  247. #ifdef ultrix
  248.  
  249. /* Ultrix version */
  250.  
  251. char *re_comp();
  252.  
  253. match(s,expr)
  254.     char *s, *expr;
  255. /*
  256.  * true if 's' matches regular expression 'expr'
  257.  * (a simple regex, grep-style, *not* egrep)
  258.  */
  259. {
  260.     static char p[256]="", *q=(char *)0;
  261.     static struct regex *r = (struct regex *)0;
  262.     if (q != expr){
  263.         if (strcmp(p,expr)){
  264.             if (r) free(r);
  265.             if (re_comp(expr))
  266.                 ;
  267.             else return 0;
  268.             strcpy(p,expr);
  269.         }
  270.         q = expr;
  271.     }
  272.     return re_exec(s,r)==1? 1: 0;
  273. }
  274. #else
  275.  
  276. /* NeXT version */
  277.  
  278. #include <regex.h>
  279.  
  280. int
  281. match(s,expr)
  282.     char *s, *expr;
  283. /*
  284.  * true if 's' matches regular expression 'expr'
  285.  * (a simple regex, grep-style, *not* egrep)
  286.  */
  287. {
  288.     static char p[256]="", *q=(char *)0;
  289.     static struct regex *r = (struct regex *)0;
  290.     if (q != expr){
  291.         if (strcmp(p,expr)){
  292.             if (r) free(r);
  293.             if (r=re_compile(expr,0))
  294.                 ;
  295.             else return 0;
  296.             strcpy(p,expr);
  297.         }
  298.         q = expr;
  299.     }
  300.     return r? re_match(s,r)==1 : 0;
  301. }
  302. #endif
  303.